package models

import (
	"github.com/jinzhu/gorm"
	_ "kop/modules/captain/configuration"
	"kop/modules/guild/entity"
	"kop/pb"
	"time"
)

//type Guild struct {
//	GuildID int32  `gorm:"primary_key;AUTO_INCREMENT"`
//	Notice  string `gorm:"size:500"`
//}

//func AutoMigrate() {
//var rows, err = dB.Model(&guild).Limit(1).Rows()
//defer rows.Close()
//if err == nil {
//	var columns, err = rows.Columns()
//	if err == nil {
//		var find = false
//		for _, column := range columns {
//			if column == "notice" {
//				find = true
//			}
//		}
//		if !find {
//			dB.Exec(`ALTER TABLE guilds ADD COLUMN "notice" character varying(500);
//			COMMENT ON COLUMN guilds.notice IS '公告';`)
//		}
//	}
//}
//}

// 随机3个公会按更新日期
func RandGuildsByUpdateDay(dB *gorm.DB, serverID int32, day int) []*pb.Guild {

	var data []entity.Guild

	if err := dB.
		Where(entity.Guild{ServerID: serverID}).
		Where("member_max > member_num").
		Where("updated_at > ?", time.Now().AddDate(0, 0, -day)).
		Order("RANDOM()").Limit(3).Find(&data).Error; err != nil {
		panic(err)
	}

	var res = make([]*pb.Guild, 0, len(data))
	for _, v := range data {
		var mess = BuildPbGuild(&v)
		res = append(res, &mess)
	}

	return res
}

// 全服随机3个公会
func RandGuilds(dB *gorm.DB, serverID int32) []*pb.Guild {

	var data []entity.Guild

	if err := dB.Where(entity.Guild{ServerID: serverID}).Where("member_max > member_num").Order("RANDOM()").Limit(3).Find(&data).Error; err != nil {
		panic(err)
	}

	var res = make([]*pb.Guild, 0, len(data))
	for _, v := range data {
		var mess = BuildPbGuild(&v)
		res = append(res, &mess)
	}

	return res
}

func GetGuilds(dB *gorm.DB, serverID, page, pageNum int32) []*pb.Guild {

	var data []entity.Guild
	if err := dB.Where(entity.Guild{ServerID: serverID}).Order(`"level" DESC`).Limit(pageNum).Offset((page - 1) * pageNum).Find(&data).Error; err != nil {
		panic(err)
	}

	var res = make([]*pb.Guild, 0, len(data))
	for _, v := range data {
		var mess = BuildPbGuild(&v)
		res = append(res, &mess)
	}
	return res
}

func GetGuild(dB *gorm.DB, serverID, guildID int32) (*entity.Guild, bool) {

	var data = entity.Guild{
		GuildID:  guildID,
		ServerID: serverID,
	}

	if err := dB.First(&data).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, false
		}
		panic(err)
	}

	return &data, true
}

func SaveGuild(dB *gorm.DB, guild *entity.Guild) {

	if err := dB.Save(guild).Error; err != nil {
		panic(err)
	}
}

func DelGuild(dB *gorm.DB, guildID int32) error {

	var data = entity.Guild{GuildID: guildID}
	return dB.Delete(&data).Error
}

func BuildPbGuild(data *entity.Guild) pb.Guild {

	return pb.Guild{
		GuildID:       data.GuildID,
		ServerID:      data.ServerID,
		Name:          data.Name,
		Flag:          data.Flag,
		Money:         data.Money,
		Exp:           data.Exp,
		Power:         data.Power,
		Level:         data.Level,
		MemberNum:     data.MemberNum,
		MemberMax:     data.MemberMax,
		AutoJoin:      data.AutoJoin,
		PresidentName: data.PresidentName,
		Declaration:   data.Declaration,
	}
}
